package com.greatchn.single_login.handler;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.http.ContentType;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @SecurityExceptionFilter: 认证异常处理
 * @author: ZBoHang
 * @time: 2023/2/9 9:06
 */
@Component
public class AuthEntryPointHandler implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {

        response.setContentType(ContentType.JSON.getValue());
        response.setCharacterEncoding(CharsetUtil.UTF_8);
        response.getWriter().write("认证异常");
    }
}
